from flask import Blueprint,render_template,g,redirect,request,jsonify
from app import db
from app.models import Product,User,Acctproduct,Applyinfo,Updatejour
import time

approval_page = Blueprint('approval_page',__name__,template_folder='templates')
@approval_page.route('/approval',methods = ['GET','POST'])
def approval():
    '''
    :return:
    '''
    if g.user is not None and g.user.is_authenticated:
        hs_current_id = str(g.user).replace("'", "")
        hs_current_user = db.session.query(User.username).filter_by(id = hs_current_id).first()[0]
        server_lists = db.session.query(Applyinfo.server_name,Applyinfo.apply_date,Applyinfo.apply_user,Applyinfo.id).filter_by(apply_status=0).filter_by(execute_user=hs_current_user).all()
        mes_lists = db.session.query(Updatejour.output,Updatejour.update_date,Updatejour.update_time,Updatejour.message,Updatejour.id,Updatejour.log_path).filter_by(read_status = 1,up_persion=hs_current_user).all()
        mes_len = len(mes_lists)
        if request.method == 'POST':
            if request.values.get('fun')=='readonly':
                hs_approve_date = str(time.strftime('%Y.%m.%d', time.localtime(time.time())))
                get_num = request.values.get('IdName')
                u = Applyinfo.query.filter_by(id=get_num).first()
                u.apply_status = 1
                u.approve_date = hs_approve_date
                u.remark = '只读权限'
                hs_acct_servername = u.server_name
                hs_acct_onwerid = u.apply_userid
                hs_acct_editrole = 0
                hs_acct_obj = Acctproduct(server_name=hs_acct_servername,server_owner_id=hs_acct_onwerid,edit_role=hs_acct_editrole)
                db.session.add(u)
                db.session.add(hs_acct_obj)
                db.session.commit()
                return "只读权限审批成功！"
            elif request.values.get('fun')=='edit':
                hs_approve_date = str(time.strftime('%Y.%m.%d', time.localtime(time.time())))
                get_num = request.values.get('IdName')
                u = Applyinfo.query.filter_by(id=get_num).first()
                u.apply_status = 1
                u.approve_date = hs_approve_date
                u.remark = '修改权限'
                hs_acct_servername = u.server_name
                hs_acct_serveraddress  = db.session.query(Product.server_address).filter_by(server_name = hs_acct_servername).first()[0]
                print(hs_acct_serveraddress)
                hs_acct_onwerid = u.apply_userid
                hs_acct_editrole = 1
                hs_acct_obj = Acctproduct(server_name=hs_acct_servername,server_owner_id=hs_acct_onwerid,edit_role=hs_acct_editrole,server_address=hs_acct_serveraddress)
                db.session.add(u)
                db.session.add(hs_acct_obj)
                db.session.commit()
                return "修改权限审批成功！"
            elif request.values.get('fun')=='refuse':
                get_num = request.values.get('IdName')
                u = Applyinfo.query.filter_by(id=get_num).first()
                db.session.delete(u)
                db.session.commit()
                return "拒绝申请成功！"
            elif request.values.get('fun')=='deletemes':
                de_id = request.values.get('mes_id')
                u = Updatejour.query.filter_by(id = de_id).first()
                u.read_status = 2
                db.session.add(u)
                db.session.commit()
    else:
        return redirect('/login')
    return render_template('hs-approval.html',hs_current_user=hs_current_user,server_lists=server_lists,mes_lists=mes_lists,mes_len=mes_len)